package com.mustafaiev.tair.autobase.stax;

import java.io.InputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class ProposesStaxParser {

	public void read(InputStream input) {
		XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
		try {
			XMLStreamReader reader = xmlInputFactory
					.createXMLStreamReader(input);
			process(reader);
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
	}

	public void process(XMLStreamReader reader) throws XMLStreamException {

		String name;
		String clientCondition;
		int id;
		long phone;

		while (reader.hasNext()) {
			int type = reader.next();
			switch (type) {
			case XMLStreamReader.START_ELEMENT:
				name = reader.getLocalName();
				switch (StaxParserEnum.valueOf(name.toUpperCase())) {
				case PROPOSES:
					id = Integer.parseInt(reader.getAttributeValue(null,
							StaxParserEnum.ID.name().toLowerCase()));
					System.out.println(id);
					break;

				case CONDITIONS:
					clientCondition = reader.getElementText();
					System.out.println(clientCondition);
				}
				// phone = Long.getLong(reader.get
				break;
			}
		}
	}
}
